package com.example.backend.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.backend.api.common.Result;
import com.example.backend.api.entity.Attendance;
import com.example.backend.api.entity.Employee;
import com.example.backend.api.mapper.AttendanceMapper;
import com.example.backend.api.service.AttendanceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xwyzsn
 * @since 2023-03-07
 */
@Service
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance> implements AttendanceService {


    @Autowired
    private AttendanceMapper attendanceMapper;

    public Result getAll(String status, Integer deptId) {
        if (status == null && deptId == null) {
            return Result.success(attendanceMapper.selectList(null));
        } else if (status != null && deptId == null) {
            return Result.success(attendanceMapper.selectList(new QueryWrapper<Attendance>().eq("status", status)));
        } else if (status == null) {
            return Result.success(attendanceMapper.selectList(new QueryWrapper<Attendance>().eq("dept_id", deptId)));
        } else {
            return Result.success(attendanceMapper.selectList(new QueryWrapper<Attendance>().eq("status", status).eq("dept_id", deptId)));
        }

    }

    public Result delete(int parseInt) {
        attendanceMapper.deleteById(parseInt);
        return Result.success(null);
    }

    public Result update(Integer id, String dept_id, String status, String endTime, String startTime) throws ParseException {
        UpdateWrapper<Attendance> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", id);
        updateWrapper.set("dept_id", dept_id);
        // convert status mapping 迟到/早退/正常
        if (status.equals("正常")) {
            status = "0";
        } else if (status.equals("迟到")) {
            status = "1";
        } else if (!status.equals("早退")) {
            status = "3";
        } else {
            status = "2";
        }
        updateWrapper.set("attendant_status", status);
        // convert start_time to Date
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTimeDate = simpleDateFormat.parse(startTime);
        updateWrapper.set("start_time", startTimeDate);
        // convert end_time to Date
        Date endTimeDate = simpleDateFormat.parse(endTime);
        updateWrapper.set("end_time", endTimeDate);
        attendanceMapper.update(null, updateWrapper);
        return Result.success(null);
    }

    public Result check(Integer id, String startTime, String endTime, Integer deptId) throws ParseException {
        if (endTime == null){
            Attendance attendance = new Attendance();
            attendance.setUserId(id);
            attendance.setDeptId(deptId);
            // convert start_time to Date
            TimeZone timeZone = TimeZone.getTimeZone("GMT+8");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            simpleDateFormat.setTimeZone(timeZone);
            Date startTimeDate = simpleDateFormat.parse(startTime);
            attendance.setStartTime(startTimeDate);
            attendanceMapper.insert(attendance);
            return Result.success(null);
        }
        else {
            QueryWrapper<Attendance> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", id);
            queryWrapper.like("start_time", startTime.substring(0, 10));
            queryWrapper.last("limit 1");
            Attendance attendance = attendanceMapper.selectOne(queryWrapper);
            if (attendance == null) {
                return Result.fail("没有找到对应的考勤记录");
            } else {
                Integer aid = attendance.getId();
                UpdateWrapper<Attendance> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("id", aid);
                // 更新考勤记录,增加结束时间
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date endTimeDate = simpleDateFormat.parse(endTime);
                updateWrapper.set("end_time", endTimeDate);
                attendanceMapper.update(null, updateWrapper);
                return Result.success(null);
            }
        }
    }

    public Result getAttendanceInfo(String start,String end) throws ParseException {

        QueryWrapper<Attendance> queryWrapper = new QueryWrapper<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = simpleDateFormat.parse(start);
        Date endDate = simpleDateFormat.parse(end);

        queryWrapper.between("start_time",startDate,endDate);
        return Result.success(attendanceMapper.selectList(queryWrapper));


    }
}
